#!/bin/sh
# 
# MAVEN工具方法
# 
# @author liuzy
# @since 2016-07-02

F_debug "F_F" "加载maven.sh"

### MAVEN下载地址
mavenDlUrl="http://mirrors.aliyun.com/apache/maven/maven-3/3.3.3/binaries/apache-maven-3.3.3-bin.tar.gz"

### MAVEN公共仓库
mavenPublicRes="/.repository"

### 默认镜相开源中国库
mavenMirrorId="OSchina"
mavenMirrorName="OSchina"
mavenMirrorUrl="http://maven.oschina.net/content/groups/public/"
mavenMirrorMirrorOf="central"

### 下载安装MAVEN并配置
## 参数
# 无
## 返回
# 无
function F_installMvn() {
	F_debug "F_installMvn" "下载安装MAVEN并配置"
	# 下载
	wget -c ${mavenDlUrl}
	if [[ ! -f "${mavenDlUrl##*/}" ]] ; then
		F_error "文件${mavenDlUrl##*/}下载失败。"
		exit 1
	fi
	# 解压
	fileName=${mavenDlUrl##*/}
	tar xf ${fileName}
	# 重命名移动
	mv ${fileName:0:18} /opt/maven
	# 配置开源中国镜相
	settings="/opt/maven/conf/settings.xml"
	echo '<?xml version="1.0" encoding="UTF-8"?>' > ${settings}
	echo '<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">' >> ${settings}
	echo '	<localRepository>/.repository</localRepository>' >> ${settings}
	echo '	<pluginGroups>' >> ${settings}
	echo '	</pluginGroups>' >> ${settings}
	echo '	<proxies>' >> ${settings}
	echo '	</proxies>' >> ${settings}
	echo '	<servers>' >> ${settings}
	echo '	</servers>' >> ${settings}
	echo '	<mirrors>' >> ${settings}
	echo '		<mirror>' >> ${settings}
	echo '			<id>'${mavenMirrorId}'</id>' >> ${settings}
	echo '			<name>'${mavenMirrorName}'</name>' >> ${settings}
	echo '			<url>'${mavenMirrorUrl}'</url>' >> ${settings}
	echo '			<mirrorOf>'${mavenMirrorMirrorOf}'</mirrorOf>' >> ${settings}
	echo '		</mirror>' >> ${settings}
	echo '	</mirrors>' >> ${settings}
	echo '	<profiles>' >> ${settings}
	echo '	</profiles>' >> ${settings}
	echo '</settings>' >> ${settings}
	# 公共仓库
	if [[ -d "/.repository" ]] ; then
		mkdir /.repository
	fi
	chmod -Rf 777 /.repository
	# 环境变量
	if [[ `id -u` = "0" ]] ; then
		F_info "为所有用户配置环境变量。"
		profile="/etc/profile"
	else
		F_info "为当前用户${USER}配置环境变量。"
		profile="${HOME}/.bash_profile"
	fi
	echo '' >> ${profile}
	echo '### config maven' >> ${profile}
	echo 'export M2_HOME=/opt/maven' >> ${profile}
	echo 'export PATH=$PATH:$M2_HOME/bin' >> ${profile}
	source ${profile}
	tail -10 ${profile}
	F_info "测试是否安装成功。"
	echo "mvn -v"
}
